package com.jiuli.liping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jiuli.liping.service.FileManageService;
import com.jiuli.liping.vo.Message;
import com.jiuli.liping.vo.Picture;
import com.jiuli.liping.vo.Product;
import com.jiuli.liping.vo.ProductDetail;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.jiuli.liping.service.ProductService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Author zyl
 * Date  2020-03-14
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/product")
public class ProductController {
    @Value("${file_hostpath}")
    private String fileHostPath;

    @Autowired
    private ProductService productService;

    @Autowired
    private FileManageService fileManageService;

    @RequestMapping(value = {"/list", ""}, method = RequestMethod.GET)
    public Object list() {
        List<Product> products = productService.findAllList();
        return products;
    }

    @RequestMapping(value = {"/get"}, method = RequestMethod.GET)
    public Object get(@RequestParam String id) {
        Product product = productService.get(id);
        return product;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public String insert(@RequestBody Product product) {
        if (productService.insert(product) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    public String insertBatch(@RequestBody List<Product> products) {
        if (productService.insertBatch(products) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@RequestBody Product product) {
        if (productService.update(product) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    /**
     * 商品删除
     * @param product
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(@RequestBody Product product) {
        if (productService.delete(product) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "/insertProduct", method = RequestMethod.POST)
    public Message insertProduct(@RequestBody Product product) {
        Message mess = productService.insertProduct(product);
        return mess;
    }


    @RequestMapping(value = "/uploadBlobImg", method = RequestMethod.POST)
    public String uploadFileImg(@RequestParam("file") MultipartFile blobFile) {
        String mess = fileManageService.uploadFileImg(blobFile);
        return mess;
    }

    @RequestMapping(value = "/deleteImg", method = RequestMethod.POST)
    public Message deleteImg(@RequestBody String  imgName) {
        Message mess = fileManageService.deleteImg(imgName);
        return mess;
    }

    /**
     * 加载下拉框和多选矿数据
     * @return
     */
    @RequestMapping(value = {"/getDetail"}, method = RequestMethod.GET)
    public ProductDetail getDetail() {
        return  productService.getDetail();
    }

    /**
     * 商品编辑
     * @param product
     * @return
     */
    @RequestMapping(value = "/updateProduct", method = RequestMethod.POST)
    public Message updateProduct(@RequestBody Product product) {
        Message mess = productService.updateProduct(product);
        return mess;
    }

    /**
     * 商品管理
     * @return
     */
    @RequestMapping(value = "/getProductList", method = RequestMethod.POST)
    public List<Product> getProductList() {
        List<Product> projectList = productService.getProductList();
        for (Product pro: projectList) {
            ArrayList<String> arr = new ArrayList<String>();
            for (Picture pic:pro.getPictureList()) {
                arr.add(pic.getPictureUrl());
            }
            pro.setPictureList2(arr);
        }
        return projectList;
    }

    /**
     * 商品首页加载
     * @return
     */
    @RequestMapping(value = "/getProductListBySj", method = RequestMethod.POST)
    public List<Product> getProductListBySj() {
        List<Product> projectList = productService.getProductListBySj();
        for (Product pro: projectList) {
            ArrayList<String> arr = new ArrayList<String>();
            for (Picture pic:pro.getPictureList()) {
                arr.add(pic.getPictureUrl());
            }
            pro.setPictureList2(arr);
        }
        return projectList;
    }

    /**
     * 商品搜索
     * @return
     */
    @RequestMapping(value = "/getProductByName", method = RequestMethod.POST)
    public List<Product> getProductByName(@RequestBody Product product) {
        return productService.getProductByName(product);
    }

    /**
     * 商品详情加载
     * @return
     */
    @RequestMapping(value = "/getProductInfo", method = RequestMethod.POST)
    public Product getProductInfo(@RequestBody Product product) {
        return productService.getProductInfo(product);
    }

    /**
     * 手机端商品上架
     * @param products
     * @return
     */
    @RequestMapping(value = "/insertBatchProduct", method = RequestMethod.POST)
    public Message insertBatchProduct(@RequestBody List<Product> products) {
        Message mes = new Message();
        if (productService.insertBatchProduct(products) > 0) {
            mes.setFlag("1");
            mes.setMessage("入库成功");
            return mes;
        } else {
            mes.setFlag("2");
            mes.setMessage("入库失败");
            return mes;
        }
    }

    /**
     * 入库单信息
     */
    @RequestMapping(value = "/findProductList", method = RequestMethod.POST)
    public Message findProductList() {
        Message mes = new Message();
        List<Product> productList = productService.findProductList();
        if (productList != null ) {
            mes.setFlag("1");
            mes.setMessage("获取入库数据成功");
            mes.setProductList(productList);
            return mes;
        } else {
            mes.setFlag("2");
            mes.setMessage("获取入库数据失败");
            return mes;
        }
    }

    /**
     * 商品上架
     * @param products
     * @return
     */
    @RequestMapping(value = "/updateBatchProduct", method = RequestMethod.POST)
    public Message updateBatchProduct(@RequestBody List<Product> products) {
        Message mes = new Message();
        if (productService.updateBatchProduct(products) > 0) {
            mes.setFlag("1");
            mes.setMessage("上架成功");
            return mes;
        } else {
            mes.setFlag("2");
            mes.setMessage("上架失败");
            return mes;
        }
    }

    /**
     * 根据商品编号获取入库单信息
     */
    @RequestMapping(value = "/findProductListByNum", method = RequestMethod.POST)
    public Message findProductListByNum(@RequestBody Product product) {
        Message mes = new Message();
        List<Product> productList = productService.findProductListByNum(product);
        if (productList != null ) {
            mes.setFlag("1");
            mes.setMessage("获取入库数据成功");
            mes.setProductList(productList);
            return mes;
        } else {
            mes.setFlag("2");
            mes.setMessage("获取入库数据失败");
            return mes;
        }
    }
}
